package library.and.xps.com.step.view.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.jude.utils.JUtils;
import library.and.xps.com.step.R;
import library.and.xps.com.step.SPConstant;
import library.and.xps.com.step.util.StepManager;
import library.and.xps.com.step.stepmodule.StepServiceDelegate;
import library.and.xps.com.step.view.customview.ColorArcProgressBar;

public class PedoViewFragment extends Fragment implements StepServiceDelegate.StepListener ,Handler.Callback{
    ColorArcProgressBar colorProgressBar;
    StepServiceDelegate stepDeleget;
    String targetStep = "";
    Handler handler;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pedoview, container, false);
        handler = new Handler(Looper.getMainLooper(),this);
        handler.sendEmptyMessageDelayed(0,3000);
        colorProgressBar = (ColorArcProgressBar) view.findViewById(R.id.pedo_pregressview);
        init();
        return view;
    }

    void init(){
        stepDeleget = StepServiceDelegate.getInstance();
        stepDeleget.setStepListener(this);

        targetStep = JUtils.getSharedPreference().getString(SPConstant.USER_STEP_PLAN, "9000");

        colorProgressBar.setTitle("今日步数:");
        colorProgressBar.setMaxValues(Integer.valueOf(targetStep));
        colorProgressBar.setUnit("目标:"+targetStep);

        if (StepManager.getInstance().getStepToday() != StepManager.STEP_NORECORD_YET) {
            stepDeleget.initStep(StepManager.getInstance().getStepToday());
            colorProgressBar.setCurrentValues(StepManager.getInstance().getStepToday());
            colorProgressBar.setTitle("今日步数:");
        } else {
            colorProgressBar.setCurrentValues(0);
            colorProgressBar.setUnit("今天还没有运动");
            colorProgressBar.setTitle("");
        }
    }

    @Override
    public void step(int step) {
        colorProgressBar.setCurrentValues(StepManager.getInstance().getStepToday());
        colorProgressBar.setTitle("今日步数:");
        colorProgressBar.setUnit("目标:"+targetStep);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch(msg.what){
            case 0:
                stepDeleget.callbindService();
                break;
        }
        return false;
    }
}
